package com.mall.biz.trade.impl.o2o.incluce;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mall.biz.trade.O2OTradeBiz;
import com.mall.client.eshop.TransferAuditService;
import com.mall.client.user.UserExtendedService;
import com.mall.client.user.UserService;
import com.mall.dto.TradePriceAndBuyCountDTO;
import com.mall.pojo.Trade;
import com.mall.pojo.TransferAudit;
import com.mall.pojo.User;
import com.mall.pojo.UserExtended;
import com.mall.pojo.UserLevel;
import com.mall.web.util.HttpUtil;
import com.mall.web.util.MoneyUtil;
import com.mall.web.util.WebUtil;

@Service
public class EshopTradeImpl implements O2OTradeBiz {
	private static final Logger log = LoggerFactory.getLogger(EshopTradeImpl.class);
	
	@Autowired
	private TransferAuditService transferAuditService;
	@Autowired
	private UserExtendedService userExtendedService;
	@Autowired
	private UserService userService;
	@Override
	public TradePriceAndBuyCountDTO pack4PirceAndBuyCount(HttpServletRequest request, Integer type, Long price,
			Integer buyCount, Long parentId) {
		TradePriceAndBuyCountDTO tradePriceAndBuyCountDTO = new TradePriceAndBuyCountDTO();
		tradePriceAndBuyCountDTO.setPrice(price);
		tradePriceAndBuyCountDTO.setBuyCount(1);
		return tradePriceAndBuyCountDTO;
	}

	@Override
	public Boolean afterSuccInsert(HttpServletRequest request, Trade trade) {
		Boolean succ = false;
		if(trade == null){
			throw new RuntimeException("transferAudit不存在");
		}
		
		Long transferAuditId = HttpUtil.getLongValueFromRequest(request, "transferAuditId");
		TransferAudit transferAudit = this.transferAuditService.getTransferAuditById(transferAuditId);
		if(transferAudit == null){
			throw new RuntimeException("transferAudit不存在");
		}
		Long balance = trade.getPrice();
		User user = this.userService.getUserByUserId(transferAudit.getUserId());
		if(user == null){
			throw new RuntimeException("transferAudit不存在");
		}
		UserExtended userExtended = this.userExtendedService.getUserExtendByUserId(transferAudit.getUserId());
		if(userExtended == null){
			userExtended = new UserExtended();
			userExtended.setUserId(user.getId());
			userExtended.setBalance(balance);
			userExtended.setBond(0l);
			this.userExtendedService.insertUserExtended(userExtended);
		}else{
			userExtended.setBalance(userExtended.getBalance()+balance);
			this.userExtendedService.updateExtendedByPrimaryKey(userExtended);
		}
		
		
		//更新审核状态
		User currentUser = WebUtil.getCurrentUser(request);
		transferAudit.setOperationUserId(currentUser.getId());
		transferAudit.setStatus(2);
		this.transferAuditService.modifyTransferAudit(transferAudit);
		succ = true;
		return succ;
	}

	@Override
	public Boolean afterErrorInsert(HttpServletRequest request, Trade trade) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Boolean beforeInsert(HttpServletRequest request, Trade trade) {
		// TODO Auto-generated method stub
		return null;
	}

}
